/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 **************************************************************************
 * MIFSS  - content storage system
 * 
 *
 * @uthors: uros.kristan@gmail.com (Uroš Kristan ) Urosk.NET
 *         jernej.svigelj@gmail.com (Jernej Švigelj) 
 */
package net.monare.mifss.managers;

import net.monare.mifss.configurations.pojo.ContentPoolDef;
import net.monare.mifss.configurations.pojo.StorageDef;
import net.monare.mifss.exceptions.ContentPoolHandlerException;
import net.monare.mifss.exceptions.PoolManagerException;
import net.monare.mifss.lib.AppContext;
import net.monare.mifss.workers.interfaces.ContentPoolHandler;
import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.stereotype.Component;

@Component
public class PoolManager {

    private static final Logger logger = Logger.getLogger(PoolManager.class);

    public void createPools(StorageDef storageDef) throws PoolManagerException {

        //
        // Create pools for given storage definition
        for (ContentPoolDef contentPoolDef : storageDef.getContentPoolDefs()) {

            ContentPoolHandler poolImpl = null;
            try {
                // get pool implementation eban from its name
                poolImpl = getContentPoolImplementation(contentPoolDef);

                // try to create pool
                poolImpl.createPool();

            } catch (BeansException be) {

                logger.error("Can not find bean definiton for pool implementation " + contentPoolDef.getImplementation());
                throw new PoolManagerException(be.getMessage(), be);

            } catch (ContentPoolHandlerException e) {
                logger.error("Error occured while trying to create new new pool:" + contentPoolDef.getTitle() + " " + e.getMessage());
                throw new PoolManagerException("Creating pool error", e);
            }

        }

    }

    public ContentPoolHandler getContentPoolImplementation(ContentPoolDef contentPoolDef) throws PoolManagerException {

        ContentPoolHandler poolImpl = null;
        try {
            // get pool implementation eban from its name
            poolImpl = (ContentPoolHandler) AppContext.getApplicationContext().getBean(contentPoolDef.getImplementation());
            poolImpl.setContentPoolDef(contentPoolDef);

            poolImpl.initializePool(contentPoolDef);

        } catch (BeansException be) {

            logger.error("Can not find bean definiton for pool implementation " + contentPoolDef.getImplementation());
            throw new PoolManagerException(be.getMessage(), be);

        } catch (ContentPoolHandlerException e) {
            logger.error("initializing pool error" + contentPoolDef.getImplementation());
            throw new PoolManagerException(e.getMessage(), e);
        }

        return poolImpl;

    }

    public void wipePoolData(ContentPoolDef contentPoolDef) throws PoolManagerException {

        ContentPoolHandler cph = getContentPoolImplementation(contentPoolDef);
        try {
            cph.wipeCompleteData(contentPoolDef);
        } catch (ContentPoolHandlerException e) {
            logger.error("Error occured while trying to remove pool:" + contentPoolDef.getTitle() + " : " + e.getMessage());
            throw new PoolManagerException(e);

        }

    }

}
